package com.wxb.qd.controller;

import com.wxb.qd.common.BaseResponse;
import com.wxb.qd.common.ErrorCode;
import com.wxb.qd.common.ResultUtils;
import com.wxb.qd.common.UserCount;
import com.wxb.qd.dto.UserEdit;
import com.wxb.qd.dto.UserRegister;
import com.wxb.qd.entity.User;
import com.wxb.qd.exception.BusinessException;
import com.wxb.qd.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

/**
 * 用户
 * @author wangxb
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;




    /**
     * 用户注册
     * @param userRegister 用户注册类
     * @return 注册成功的id
     */
    @PostMapping("/register")
    public BaseResponse<Integer> userRegister(@RequestBody UserRegister userRegister){
        if (userRegister == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户为空");
        }
        String userAccount = userRegister.getUserAccount();
        String userPassword = userRegister.getUserPassword();
        String checkPassword = userRegister.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount,userPassword,checkPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"输入错误");
        }
        int userId = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(userId);
    }

    /**
     * 用户登录
     * @param user 用户账号密码
     * @param request 请求
     * @return 用户信息
     */
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody User user, HttpServletRequest request){
        if (user == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户为空");
        }
        String userAccount = user.getUserAccount();
        String userPassword = user.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount,userPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号或密码为空");
        }
        User loginUser = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(loginUser);
    }

    /**
     * 获取登录的用户
     * @param request 请求
     * @return 登录的用户信息
     */
    @GetMapping("/get/login")
    public BaseResponse<User> getLoginUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(loginUser);
    }

    /**
     * 用户更新
     * @param userEdit 用户可编辑的信息
     * @return 是否更新成功
     */
    @PostMapping("/edit")
    public BaseResponse<Boolean> userEdit(@RequestBody UserEdit userEdit){
        if (userEdit == null || userEdit.getId() == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user =  new User();
        BeanUtils.copyProperties(userEdit,user);
        boolean result = userService.updateById(user);
        return ResultUtils.success(result);
    }

    /**
     * 用户退出
     * @param request 请求
     * @return 退出成功
     */
    @GetMapping("/logout")
    public BaseResponse<Boolean> userLogOut(HttpServletRequest request){
        boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 获取所有用户数量信息
     * @return
     */
    @GetMapping("/all")
    public BaseResponse<List<UserCount> > getAllUserAndHasAeroData(){
        List<UserCount> allUserAndHasAeroData = userService.getAllUserAndHasAeroData();
        return ResultUtils.success(allUserAndHasAeroData);
    }

    /**
     * 修改用户头像，未完成
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/avatar")
    public BaseResponse<Boolean> changeUserAvatar(@RequestParam MultipartFile file,HttpServletRequest request){
        if (file.isEmpty()){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"上传文件为空");
        }
        String filename = file.getOriginalFilename();
        //todo 先保存到本地 但是无法展示 需要修改保存到本地之后上传到gitee 才能展示
        String localURL = "E:\\MLwxb\\qd\\src\\main\\resources\\save\\";
        String filePath = localURL + filename;
//        String filePath =giteeURL + filename;
        try{
            file.transferTo(new File(filePath));
            User loginUser = userService.getLoginUser(request);
            if (loginUser == null){
                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
            }
            loginUser.setUserAvatar(filePath);
            userService.updateById(loginUser);
        }catch (Exception e){
            e.printStackTrace();
        }

        return ResultUtils.success(true);
    }
}













